package me.zd.tool.hardlink.controller.impl;

import me.zd.tool.hardlink.model.FullSetting;
import me.zd.tool.hardlink.service.ConfigService;
import me.zd.tool.hardlink.service.I18nService;
import me.zd.tool.hardlink.service.PropertiesService;
import me.zd.tool.hardlink.controller.IStartupController;
import me.zd.tool.hardlink.service.HardLinkService;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class CMDController implements IStartupController {

    private final FullSetting setting = new FullSetting();

    @Override
    public void startup(Map<String, String> argMap) {
        if (!argMap.containsKey("c")) {
            System.out.println(I18nService.INS.format("config.load.failed", "null"));
            return;
        }
        try {
            boolean result = ConfigService.INS.importSetting(setting, argMap.get("c"), false, System.out::println);
            if (!result) {
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        ConfigService.INS.properties2Setting(PropertiesService.toProperties(argMap), setting, false);

        // 执行链接
        try {
            if (argMap.containsKey("test")) {
                Map<File, File> linkedFileMap = HardLinkService.INS.createHardLink(setting.link,
                        HardLinkService.TEST_MODE_ALL, System.out::println);
                if (!linkedFileMap.isEmpty()) {
                    linkedFileMap.forEach((file, destFile) -> System.out.println(I18nService.INS.format("processing.test.linked",
                            file.getAbsolutePath(), destFile.getAbsolutePath())));
                    System.out.println(I18nService.INS.format("processing.test.done", linkedFileMap.size()));
                }
            } else if (argMap.containsKey("testOne")) {
                HardLinkService.INS.createHardLink(setting.link, HardLinkService.TEST_MODE_FIRST_ONLY, System.out::println);
            } else {
                HardLinkService.INS.createHardLink(setting.link, HardLinkService.TEST_MODE_OFF, System.out::println);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
